package com.service.impl;

import com.dao.*;
import com.entity.PostingInfo;
import com.entity.ReplyAgainInfo;
import com.entity.ReplyInfo;
import com.entity.UserInfo;
import com.service.PostingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 发表帖子业务实现类
 *
 * @author lbb
 * @version 1.0
 * @date 2020.3.1
 */
@Service
public class PostingInfoServiceImpl implements PostingInfoService {
	//帖子
	@Autowired
	private PostingInfoDao postingInfoDao;
	//权限
	@Autowired
	private PostingVisitPrivilegeDao postingVisitPrivilegeDao;
	//回复
	@Autowired
	private ReplyInfoDao replyInfoDao;
	//再次回复
	@Autowired
	private ReplyAgainInfoDao replyAgainInfoDao;
	//点赞
	@Autowired
	private PostingLikeDao postingLikeDao;
	//用户
	@Autowired
	private UserInfoDao userInfoDao;

	//发帖
	@Override
	@Transactional
	public Boolean addPostingInfo(PostingInfo postingInfo, Integer... userIds) {
		if (postingInfoDao.addPostingInfo(postingInfo)) {
			return postingVisitPrivilegeDao.addPostingVisitPrivilege(postingInfo.getId(), userIds);
		}
		return false;
	}
	
	//查看全部贴
	@Override
	@Transactional
	public List<PostingInfo> getUserCanSeePostingInfo(Integer userId,Integer isAddBrowseCount) {
		//获得所有用户可以访问的帖子
		List<PostingInfo> postingInfoList = postingInfoDao.getVisitPostingAll(userId);

		for (PostingInfo postingInfo : postingInfoList) {


			//查询发帖用户信息
			UserInfo fromUserInfo = userInfoDao.getById(postingInfo.getFromUserId());
			postingInfo.setFromUserInfo(fromUserInfo);


			//判断如果转发id不为0，则查询一下转发信息
			if(!(postingInfo.getForwardId().equals(0))){
				//转发帖子
				PostingInfo forwardPostingInfo = postingInfoDao.getPostingInfoById(postingInfo.getForwardId());
				//转发帖子的用户信息
				UserInfo forwardUserInfo = userInfoDao.getById(forwardPostingInfo.getFromUserId());
				forwardPostingInfo.setFromUserInfo(forwardUserInfo);
				postingInfo.setForWordPostingInfo(forwardPostingInfo);
			}

			if(isAddBrowseCount != 0){
				//浏览次数加1
				postingInfo.setBrowseCount(postingInfo.getBrowseCount()+1);
				postingInfoDao.editPostingInfo(postingInfo);
			}

			//查询所有点赞用户信息
			List<UserInfo> likeUserInfo = userInfoDao.getLikesByPosting(postingInfo.getId());
			postingInfo.setLikeUserInfo(likeUserInfo);

			//全部回复
			List<ReplyInfo> replyInfos = replyInfoDao.getAllPostingInfo(postingInfo.getId());
			for (ReplyInfo replyInfo : replyInfos) {
				//查询发回复的用户信息
				UserInfo replyFromUserInfo = userInfoDao.getById(replyInfo.getFromUserId());
				replyInfo.setFromUserInfo(replyFromUserInfo);

				//全部再次回复
				List<ReplyAgainInfo> replyAgainInfos = replyAgainInfoDao.getAllReplyAgainInfo(replyInfo.getId());
				for (ReplyAgainInfo replyAgainInfo : replyAgainInfos) {
					//查询发再次回复的用户信息
					UserInfo replyAgainFromUserInfo = userInfoDao.getById(replyAgainInfo.getFromUserId());
					replyAgainInfo.setFromUserInfo(replyAgainFromUserInfo);
					//查询收再次回复的用户信息
					UserInfo replyAgainToUserInfo = userInfoDao.getById(replyAgainInfo.getToUserId());
					replyAgainInfo.setToUserInfo(replyAgainToUserInfo);
				}
				replyInfo.setReplyAgainInfoList(replyAgainInfos);
			}
			postingInfo.setReplyInfoList(replyInfos);

		}
		return postingInfoList;
	}
	//删除帖子
	@Override
	@Transactional
	public Boolean delPostingInfo(Integer postingId, Integer userId) {
		//删除再次回复
		List<Integer> replyInfoList = replyInfoDao.getAllReplyInfoId(postingId);
		for (Integer integer : replyInfoList) {
			replyAgainInfoDao.delAllReplyAgainInfo(integer);
		}
		//删除回复
		replyInfoDao.delAllReplyInfo(postingId);
		//删除点赞
		postingLikeDao.delAllPostingLike(postingId);
		//删除权限
		postingVisitPrivilegeDao.delAllPostingVisitPrivilege(postingId);
		//删除帖子
		Boolean bool = postingInfoDao.delPostingInfo(postingId,userId);
		
		return bool;
	}
	
}
